﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Pampa.CRUD.Configuration;
using Pampa.CRUD.Core;

namespace Pampa.MVC.Fluent.Crud.Groups
{
    public class DetailsGroupBuilder<TGroup, TEditor> : GroupBuilder<TGroup, TEditor>
    {
        private readonly DetailsGroup _detailsGroup;

        public DetailsGroupBuilder(EntityConfig<TEditor> entityConfig, DetailsGroup group) : base(entityConfig, group)
        {
            _detailsGroup = group;
        }

        public DetailsGroupBuilder<TGroup, TEditor> Title(string title)
        {
            _detailsGroup.Title = title;
            return this;
        }

        public DetailsGroupBuilder<TGroup, TEditor> AddEditPermission(Func<TEditor, bool> action)
        {
            var guid = Guid.NewGuid().ToString();
            this.ParentEditor.SecurityManager.AddEditRule(guid, action);
            _detailsGroup.PermissionRequired = guid;
            return this;
        }

        public DetailsGroupBuilder<TGroup, TEditor> AddViewPermission(Func<TEditor, bool> action)
        {
            var guid = Guid.NewGuid().ToString();
            this.ParentEditor.SecurityManager.AddViewRule(guid, action);
            _detailsGroup.PermissionRequired = guid;
            return this;
        }

        public DetailsGroupBuilder<TGroup, TEditor> AddCustomPermission(string permision)
        {
            _detailsGroup.PermissionRequired = permision;
            return this;
        }

        public DetailsGroupBuilder<TGroup, TEditor> RemoveButton(bool value, string text = null, string confirmation = null)
        {
            _detailsGroup.RemoveButton = value;
            _detailsGroup.DeleteButtonText = text;
            _detailsGroup.DeleteConfirmation = confirmation;
            return this;
        }

        public DetailsGroupBuilder<TGroup, TEditor> CloneButton(bool value, string text = null)
        {
            _detailsGroup.CloneButton = value;
            _detailsGroup.CloneButtonText = text;
            return this;
        }

        public DetailsGroupBuilder<TGroup, TEditor> AddButton(bool value, string text = null, string image = null) 
        {
            _detailsGroup.AddButton = value;
            _detailsGroup.AddButtonText = text;
            _detailsGroup.AddButtonImage = image;

            return this;
        }

        public DetailsGroupBuilder<TGroup, TEditor> AddItemAction(string icon, string text, string action, bool ignoreParentPermissions = false)
        {
            var itemLink = new ItemLink();
            itemLink.IconPath = icon;
            itemLink.Text = text;
            itemLink.Action = action;

            if (ignoreParentPermissions)
            {
                var guid = Guid.NewGuid().ToString();
                this.ParentEditor.SecurityManager.AddEditRule(guid, m => true);
                itemLink.PermissionRequired = guid;
            }

            _detailsGroup.ItemLinks.Add(itemLink);

            return this;
        }

        public DetailsGroupBuilder<TGroup, TEditor> ActionsText(string text) 
        {
            _detailsGroup.ActionsText = text;
            return this;
        }
        
        public DetailsGroupBuilder<TGroup, TEditor> OnlyVisibles()
        {
            _detailsGroup.OnlyVisibles = true;
            return this;
        }

        /// <summary>
        /// Allows to override delete action.
        /// Action returns true if the entity repository delete action should be called after this.
        /// </summary>
        public void OnDelete(Expression<Func<TGroup, bool>> action)
        {
            EntityManager.OnDelete(action);
        }
    }
}
